﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.IO;
using MetaNote.Controls;

namespace MetaNote.Model
{
    public class Document
    {
        public string FileName { get; set; }

        [XmlIgnore()] 
        public string Content { get; set; }

        public bool IsDirty { get; set; }

        //todo; make some use of document types (?)
        public string DocumentType { get; set; }

        public string Title { get; set; }

        [XmlIgnore()]
        public NiceTextBox CurrentTextBox { get; set; }

        public Document(Guid key, int seq, string title) : this()
        {
            this.Key = key;
            this.Seq = seq;
            this.Title = title;
        }

        /// <summary>
        /// The old 'default constructor', necessary for serialization purposes.
        /// </summary>
        public Document()
        {
        }

        [XmlIgnore()]
        public string TabName
        {
            get
            {
                //"(untitled)"
                return (this.FileName != null 
                        ? Path.GetFileName(this.FileName) 
                        : (this.Title ?? "(untitled[" + Seq.ToString() + "])" )) 

                        + 

                        (this.IsDirty 
                        ? " *" 
                        : string.Empty);
            }
        } 

        public Guid Key { get; private set; }

        public int Seq { get; private set; }

        //Someday: Maybe documents should include an extensible property list.
        //Someday: Maybe document types should include a (persisted) extensible property list.

        internal bool Saveas(Form that)
        {
            DialogResult result = DialogResult.Cancel;
            var saveFile = new SaveFileDialog();
            saveFile.Title = GetDialogTitle(this.TabName);
            result = saveFile.ShowDialog(that);

            if (result == DialogResult.OK)
            {
                this.FileName = saveFile.FileName;                
                return Save();
            } else {
                return false;
            }
        }

        private string GetDialogTitle(string fileName)
        {
            return "MetaNote - " + fileName;
        }

        internal bool Save(Form that)
        {
            if (this.FileName == null)
            {
                return Saveas(that);
            }
            else
            {
                return Save();
            }
        }

        private bool Save()
        {
            string text = CurrentTextBox.Text;
            using (var oWrite = File.CreateText(this.FileName))
            {
                oWrite.Write(text);
            }
            this.IsDirty = false;
            return true;
        }

        internal bool Open(MainForm that)
        {
            DialogResult result = DialogResult.Cancel;
            var openFile = new OpenFileDialog();
            result = openFile.ShowDialog(that);
            if (result == DialogResult.OK)
            {
                this.FileName = openFile.FileName;
                return Open();
            }
            return false;
        }

        private bool Open()
        {
            using (var oRead = File.OpenText(this.FileName))
            {
                CurrentTextBox.Text = oRead.ReadToEnd(); 
            }
            this.IsDirty = false;
            return true;
        }

        public override string ToString()
        {
            return this.TabName;
        }

        internal bool Open(string fileName)
        {
            this.FileName = fileName;
            return Open();            
        }
    }
}
